package base.tf {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.geom.Matrix;
	import flash.text.TextFormat;
	
	public class GradientTextField extends Sprite implements ITextField {
		
		public var tf: TextField;
		private var maskedTf: Sprite;
		private var colors: Array;
		private var alphas: Array;
		private var ratios: Array;
		private var rotationGradient: Number;
		private var gradientType: String;
		private var tx: Number;
		private var ty: Number;
		
		public function GradientTextField(tf: TextField, colors: Array, alphas: Array, ratios: Array, rotationGradient: Number = 0, gradientType: String = "linear", tx: Number = 0, ty: Number = 0): void {
			this.tf = tf;
			this.colors = colors;
			this.alphas = alphas;
			this.ratios = ratios;
			this.rotationGradient = rotationGradient;
			this.gradientType = gradientType;
			this.tx = tx;
			this.ty = ty;
			this.maskedTf = new Sprite();
			this.addChild(this.maskedTf);
			this.addChild(tf);
			this.maskedTf.mask = tf;
			this.redraw();
		}
		
		private function redraw(): void {
			this.tf.x = (this.tf.textWidth - this.tf.width) / 2;
			this.tf.y = (this.tf.textHeight - this.tf.height) / 2;
			var mrxTf: Matrix = new Matrix()
			mrxTf.createGradientBox(tf.textWidth, tf.textHeight, this.rotationGradient, this.tx, this.ty);
			this.maskedTf.graphics.clear();
			this.maskedTf.graphics.beginGradientFill(this.gradientType, this.colors, this.alphas, this.ratios, mrxTf);
			this.maskedTf.graphics.drawRect(0, 0, tf.textWidth, tf.textHeight);
			this.maskedTf.graphics.endFill();
		}
		
		public function get text(): String {
			return this.tf.text;
		}
		
		public function set text(value: String): void {
			this.tf.text = value;
			this.redraw();
		}
		
		public function get htmlText(): String {
			return this.tf.htmlText;
		}
		
		public function set htmlText(value: String): void {
			this.tf.htmlText = value;
			this.redraw();
		}
		
		public function get autoSize(): String {
			return this.tf.autoSize;
		}
			
		public function set autoSize(value: String): void {
			this.tf.autoSize = value;
			this.redraw();
		}

		public function get textWidth(): Number {
			return this.maskedTf.width;
		}
			
		override public function get width(): Number {
			return this.maskedTf.width;
		}

		override public function get height(): Number {
			return this.maskedTf.height;
		}

		public function getTextFormat(): TextFormat {
			return this.tf.getTextFormat();
		}
		
		public function setTextFormat(value: TextFormat): void {
			this.tf.setTextFormat(value);
			this.redraw();
		}
		
		public function get defaultTextFormat(): TextFormat {
			return this.tf.defaultTextFormat;
		}
		
		public function set defaultTextFormat(value: TextFormat): void {
			this.tf.defaultTextFormat;
			this.redraw();
		}
		
		public function destroy(): void {
			this.removeChild(this.maskedTf);
			this.maskedTf = null;
			this.removeChild(this.tf);
			this.tf = null;
		}
		
	}

}